package spider;

import java.util.ArrayList;
import java.util.List;

import download.DownLoadThread;
import frontier.LinkQueue;

import util.*;

public class Spider {

	Spider(List<String> seedList, LinkQueue linkQueue) {
		for (int i = 0; i < seedList.size(); i++)
			linkQueue.addUnvisitedUrl(seedList.get(i));
	}

	// main 方法入口
	public static void main(String[] args) {
		
		try {
			
			
			//初始化种子
			LinkQueue linkQueue = new LinkQueue();
			MyXmlUtil seedxml = new MyXmlUtil("seeds.xml");
			List<String> seedList = seedxml.getValues("site");
			Spider crawler = new Spider(seedList, linkQueue);

			
			List<Thread> threadList = new ArrayList<Thread>();
			MyXmlUtil configure = new MyXmlUtil("configure.xml");
			int threadNum = Integer.parseInt(configure.getConfigure("thread", "num"));
		
			for (int i = 1; i <= threadNum; i++) {
				DownLoadThread childDownloader = new DownLoadThread("Spider #"
						+ i + ":", linkQueue);
				threadList.add(childDownloader.thrd);
				childDownloader.thrd.start();

			}

			while (threadList.size() > 0) {
				Thread child = (Thread) threadList.remove(0);
				child.join();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("爬取网页完成");
	}

}